﻿// 加载函数
$().ready(function () {
    Code();
    CodeChange();
    GetUserPhone();
    GetUserPwd();
    Denglu();
});

/*监听用户名账号*/
function GetUserPhone()
{
    //判断账号是否非空
    $("#UserPhoneInput").bind("input propertychange", function () {
        if ($(this).val() != "") {
            $("#UserPhoneMasgg").css("visibility", "hidden");
        }
        else {
            $("#UserPhoneMasgg").css("visibility", "visible").html("还没告诉我你的手机号");
        }
    });
}

//判断密码是否为空
function GetUserPwd()
{
    $("#UserPwd").bind("input propertychange", function () {
        if ($(this).val() != "") {
            $("#UserPwdMasgg").css("visibility", "hidden");
        }
        else {
            $("#UserPwdMasgg").css("visibility", "visible").html("密码不能为空");
        }
    });
}


//登录按钮
function Denglu()
{
    $("#denglu").click(function () {
        var count = 0;
        
         if ($("#Code").val() == "")
         {
            count++;
            $("#CodeMasgg").css("visibility", "visible").html("请输入验证码");
         }
         else if ($("#Code").val() != $("#Code-button").text()) {
            $("#CodeMasgg").html("验证码有误").css("visibility", "visible");
            count++;
         }

        if ($("UserPwd").val() == "")
        {
            $("#UserPwdMasgg").css("visibility", "visible").html("密码不能为空").focus();
            count++;
        }

        if ($("UserPhoneInput").val() == "")
        {
            count++;
            $("#UserPhoneMasgg").css("visibility", "visible").html("还没告诉我你的手机号");
        }

        if (count == 0)
        {
            var x = 0;
            if ($("#check-box").is(':checked')) {
                x = 1;
            }
            else
            {
                x = 2;
            }

            $.ajax({
                url: 'Login.ashx',
                data: { cheBox: x, UserPhone: $("#UserPhoneInput").val(), UserPwd: $("#UserPwd").val() },
                type: 'POST',
                success: function (data) {
                    console.log(data);
                    switch (data) {
                        case "1":
                            location.href = "Homepage.aspx";
                            break;
                        case "2":
                            alert("用户或密码有误");
                            break;
                    }
                }
            });
        }

    });
}


//判断验证码状态
function CodeChange() {
    $("#Code").bind("input propertychange", function () {
        var word = $(this).val().trim();
        if (word.length > 4) {
            word = word.substring(0, 4);
        }
        if (word.length == 0) {
            $("#CodeMasgg").css("visibility", "visible");
            if ($("#CodeMasgg").html() != "请输入验证码") { $("#CodeMasgg").html("请输入验证码"); }
        }
        else {
            $("#CodeMasgg").css("visibility", "hidden");
        }
        $(this).val(word);
    });
}

/*获取验证码*/
function Code()
{
    $("#Code-button").click(function () {
        if ($(this).text() == "验证码")
        {
            var number = Math.floor(Math.random() * 9000 + 999);
            $(this).html(number);
            console.log(number);
        }
    });
}